/**
 * PHOSIDE: PHosphorylation Site IDentification Engine.
 * Copyright 2009 Digital Biology Lab, University of Missouri.
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 3 of the License, or (at your option) any later
 * version. <p/> This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the License for more
 * details.
 */

package phoside.data.nr;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import phoside.Protein;
import phoside.Proteins;

import phoside.data.util.PhosphoDataUtil;

/**
 *
 * @author gjj
 */
public class ProteinNR100Builder extends ProteinNRBuilderImpl {

    public ProteinNR100Builder() {
        super(new ProteinClusterSameSequence(), new ProteinSelectorMergeSites());
    }

    private static class ProteinClusterSameSequence implements ProteinCluster {
        public List<List<Protein>> build(Proteins proteins) {
            HashMap<String,List<Protein>> map = new HashMap();
            Iterator<Protein> it = proteins.proteinIterator();
            while (it.hasNext()) {
                Protein protein = it.next();
                String seq = protein.getSequence();

                List<Protein> curr = map.get(seq);
                if (curr==null) {
                    curr = new ArrayList();
                    map.put(seq, curr);
                }

                curr.add(protein);
            }

            return new ArrayList(map.values());
        }
    }

    private static class ProteinSelectorMergeSites implements ProteinSelector {
        /**
         * {@inheritDoc}
         */
        public Protein select(List<Protein> proteinCluster) {
            if (proteinCluster==null || proteinCluster.isEmpty()) {
                return null;
            }

            Protein ret = proteinCluster.get(0);

            int n = proteinCluster.size();
            for (int i=1; i<n; i++) {
//                Set<Integer> sites = PhosphoDataUtil.getSites(proteinCluster.get(i));
//                if (sites!=null && !sites.isEmpty()) {
//                    PhosphoDataUtil.addSites(ret, sites);
//                }
                ret = PhosphoDataUtil.mergeProteins(ret, proteinCluster.get(i));
            }

            return ret;
        }
    }
}
